package com.lemon.smartsafety.utils.common;

import android.os.Handler;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;

import com.daimajia.androidanimations.library.Techniques;
import com.daimajia.androidanimations.library.YoYo;

/**
 * Created by Cooliean on 16/4/1.
 */
public class AnimationHelper {
    private              View view  = null;
    private final static Long SPEED = 1500L;
    //这里是灯的闪烁时间间隔
    public final static int STATUS_IMAGE_RED_DELAY = 1000;
    public final static int STATUS_IMAGE_YELLOW_DELAY = 2500;

    public AnimationHelper(View view) {
        this.view = view;
    }

    private AnimationHelper() {
    }

    public void startAnimation() {
        startAnimation(SPEED);
    }

    public void startAnimation(Long second) {
        //创建AnimationSet对象
        AnimationSet animationSet = new AnimationSet(true);
        //创建RotateAnimation对象
        RotateAnimation rotateAnimation = new RotateAnimation(0f, +359f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        //设置动画持续
        rotateAnimation.setDuration(second);
        rotateAnimation.setRepeatCount(-1);
        rotateAnimation.setRepeatMode(Animation.RESTART);
        //动画插入器
        //        rotateAnimation.setInterpolator(context, android.R.anim.linear_interpolator);
        //添加到AnimationSet
        animationSet.addAnimation(rotateAnimation);
        //setInterpolator表示设置旋转速率。LinearInterpolator为匀速效果，Accelerateinterpolator为加速效果、DecelerateInterpolator为减速效果，具体可见下面android:interpolator的介绍。
        LinearInterpolator lin = new LinearInterpolator();
        animationSet.setInterpolator(lin);
        //开始动画
        view.startAnimation(animationSet);

    }

    public static void startAlphaAnimation(View view) {
        startAlphaAnimation(view, 5000);
    }

    public static void startAlphaAnimation(View view, int delay) {
        //闪烁
        AlphaAnimation alphaAnimation1 = new AlphaAnimation(0.1f, 1.0f);
        alphaAnimation1.setDuration(delay);
        alphaAnimation1.setRepeatCount(Animation.INFINITE); //表示重复多次。 也可以设定具体重复的次数，比如alphaAnimation1.setRepeatCount(5);
        alphaAnimation1.setRepeatMode(Animation.REVERSE);//表示动画结束后，反过来再执行。 该方法有两种值， RESTART 和 REVERSE。 RESTART表示从头开始，REVERSE表示从末尾倒播。
        view.setAnimation(alphaAnimation1);
        alphaAnimation1.start();
    }

    public static void stopAlphaAnimation(View view) {
        //闪烁
        AlphaAnimation alphaAnimation1 = new AlphaAnimation(1.0f, 1.0f);
        alphaAnimation1.setDuration(Integer.MAX_VALUE);
        alphaAnimation1.setRepeatCount(Animation.INFINITE); //表示重复多次。 也可以设定具体重复的次数，比如alphaAnimation1.setRepeatCount(5);
        alphaAnimation1.setRepeatMode(Animation.REVERSE);//表示动画结束后，反过来再执行。 该方法有两种值， RESTART 和 REVERSE。 RESTART表示从头开始，REVERSE表示从末尾倒播。
        view.setAnimation(alphaAnimation1);
        alphaAnimation1.start();
    }

    public static void startAlphaAnimationWithYoYo(final View view, final int delay) {
        final Handler handler =new Handler();
        Runnable runnable=new Runnable() {
            @Override
            public void run() {
                //要做的事情
                YoYo.with(Techniques.Flash).duration(delay).playOn(view);

                handler.postDelayed(this, delay+1000);
            }
        };
        handler.postDelayed(runnable, delay+1000);//每两秒执行一次runnable.
    }

}
